import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)


class evadeObstacle(object):
    def __init__(self):

        self.InfraredPinLeft = 4
        self.InfraredPinRight = 18
        GPIO.setup(self.InfraredPinLeft, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(self.InfraredPinRight, GPIO.IN, pull_up_down=GPIO.PUD_UP)


    def infra_detect(self):
        infra_left_value = 1
        infra_right_value = 1
        if (0 == GPIO.input(self.InfraredPinLeft)):  # 当检测到障碍物时，输出低电平信号
            infra_left_value = 0
        if (0 == GPIO.input(self.InfraredPinRight)):  # 当检测到障碍物时，输出低电平信号
            infra_right_value = 0
        return infra_left_value, infra_right_value

    def destroy(self):
        GPIO.cleanup()


if __name__ == "__main__":

    try:
        evadeObstacle = evadeObstacle()
        while True:
            infra_left_value, infra_right_value = evadeObstacle.infra_detect()
            print('infra_left_value=%d infra_right_value=%d' % (infra_left_value, infra_right_value))
            time.sleep(0.1)
    except KeyboardInterrupt:
        evadeObstacle.destroy()
